
package com.ymx.sdk.animator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;

/**
 * use hardware layers to improve android animation performance
 * 
 * @author KongTao
 */
@SuppressLint("NewApi")
public class LayerEnableAnimatorListenerAdapter extends AnimatorListenerAdapter {

    private final View mTargetView;

    private int mLayerType;

    public LayerEnableAnimatorListenerAdapter(View targetView) {
        mTargetView = targetView;
    }

    public View getTargetView() {
        return mTargetView;
    }

    @Override
    public void onAnimationStart(Animator animation) {
        super.onAnimationStart(animation);
        if (Build.VERSION.SDK_INT >= 11 && mTargetView != null) {
            mLayerType = mTargetView.getLayerType();
            mTargetView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        if (Build.VERSION.SDK_INT >= 11 && mTargetView != null) {
            mTargetView.setLayerType(mLayerType, null);
        }
    }
}
